$(document).ready(function () {
	var urleid =getParameterByName("id")
  console.log(urleid);
    sankofaeventinfo.loadeventinfo(urleid);

    sankofaEventVolunteerinfo.loadvolunteerinfo(urleid);

    sankofaEventPeopleAttendingVolunteerinfo.loadEventVolunteer(urleid);
    console.log("loaded")

});

var urleid = getParameterByName("id")


function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

sankofaeventinfo = {
     
    // Set twitter username, number of tweets & id/class to append tweets

    // core function of jqtweet
    // Add event to table under event edit
    addeventsinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/events',
            type: 'POST',
            dataType: 'json',

            data:{
               

                ename: $('#ename'+i).val(),
                ebegindate: $('#ebegindate'+i).val(),
                eenddate: $('#eenddate'+i).val(),
                edescription: $('#edescription'+i).val(),
                
            },
            //Post into squlite3
            success: function(data, textStatus, xhr) {
                Console.log('Load was performed.')
                
            //   window.location = "addevent.html";
            //   var html =  '<div class="tweet">TWEET_TEXT<div class="time">AGO</div>';
            //   $(sankofaevent.appendTo).append( html.replace('TWEET_TEXT', data[i].text)
            // .replace('AGO', data[i].created_at)
              
            }
        });
    },

            
//       $(JQTWEET.appendTo).append("<div>SUCCESS</div>" + data.length);    

               
         
      //            // append tweets into page
      //            for (var i = 0; i < data.length; i++) {
      //               $(sankofaevent.appendTo).append(
      //                   html.replace('TWEET_TEXT', data[i].text)
      // .replace('AGO', data[i].created_at)
      //               );
            //      }                  

            // }   

         // });
      

    //Load events onto the page
    loadeventinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/events',
            type: 'GET',
            dataType: 'json',

            // i is goin got be the number of the event
            

            success: function(data, textStatus, xhr)
            
              {
                //function addTableRow(table)
                
                // clone the last row in the table
                 //var $tr = $('#eventnametable').find("tbody tr:last").clone();
                 //$tr.find("input").attr("value","");
             
                // get the name attribute for the input and select fields
                //var dataa = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'];
                
                    //make the td
                    for (j = 0; j < data.length; j++ ){
                    if (data[j].eid == i){
                 $("#eventinfotable").find("#nvalue").addClass("basicstyle col-1").html(data[j].ename)
                 $("#eventinfotable").find("#sdvalue").addClass("basicstyle col-2").html(data[j].ebegindate)
                 $("#eventinfotable").find("#edvalue").addClass("basicstyle col-3").html(data[j].eenddate)
                 $("#eventinfotable").find("#edescription").addClass("basicstyle col-4").html(data[j].edescription)
                  }                
                  }
                  }
                             
            });           
                 //console.log("LOADED");
            }   
            }   
    
sankofaEventVolunteerinfo = {
     
    // Set twitter username, number of tweets & id/class to append tweets

    // core function of jqtweet
    // Add event to table under event edit
    addEventVolunteerinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/EventVolunteer',
            type: 'POST',
            dataType: 'json',

            data:{
               

                ename: $('#ename'+i).val(),
                ebegindate: $('#ebegindate'+i).val(),
                eenddate: $('#eenddate'+i).val(),
                eedescription: $('#edescription'+i).val(),
                
            },
            //Post into squlite3
            success: function(data, textStatus, xhr) {
                Console.log('Load was performed.')
                
            //   window.location = "addevent.html";
            //   var html =  '<div class="tweet">TWEET_TEXT<div class="time">AGO</div>';
            //   $(sankofaevent.appendTo).append( html.replace('TWEET_TEXT', data[i].text)
            // .replace('AGO', data[i].created_at)
              
            }
        });
    },

    loadvolunteerinfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/volunteers',
            type: 'GET',
            dataType: 'json',

            // i is goin got be the number of the event
            

            success: function(data, textStatus, xhr){



              for (p = 0; p< data.length; p++){
              var $cb =$('<input type="checkbox" />').attr("id", data[p].vid)
              var $tr = $('<div> </div>').addClass('basicstyle col-1').html(data[p].vlastname + ", " + data[p].vfirstname).append($cb)
              var $div = $('<div> </div>').append($tr).addClass('row')

                 $("#volunteernames").append($div)
               }
               }                  
            });           
                 //console.log("LOADED");
            }   

   }         

sankofaEventPeopleAttendingVolunteerinfo = {


  loadEventVolunteer: function(urlid) {
  $.ajax({
    url: 'http://localhost:4567/EventVolunteer/'+urlid,
    type:'GET',
    dataType: 'json',

    success:function(data,textStatus, xhr){
    console.log("for loop")
    console.log(data.length)
     for (p = 0; p < data.length; p++){
     var $evname = $('<div> </div>').addClass("evdata").html(data[p].vlastname + ", " + data[p].vfirstname + ", " + data.length)

     $("#eventVolunteerinfo").append($evname)
   }
    }
  });
},
//bringing id
 addPeopleattendinginfo: function(i) {
        $.ajax({
            url: 'http://localhost:4567/EventVolunteer',
            type: 'POST',
            dataType: 'json',
            
             data:{
              eid: urleid,
              vid: i,
              ehours: "101",
              edescription: "EVENT"
            },
      


success: function(data, textStatus, xhr) {
                Console.log('Load was performed.')


}});
}
}
//       $(JQTWEET.appendTo).append("<div>SUCCESS</div>" + data.length);    

               
         
      //            // append tweets into page
      //            for (var i = 0; i < data.length; i++) {
      //               $(sankofaevent.appendTo).append(
      //                   html.replace('TWEET_TEXT', data[i].text)
      // .replace('AGO', data[i].created_at)
      //               );
            //      }                  

            // }   